package net.godbirds.boot.common.filter;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
/**
 * 对外开放服务还需要一些安全措施来保护客户端只能访问它应该访问到的资源<br>
 * 在主类中添加 @bean
	public AccessFilter accessFilter() {
		return new AccessFilter();
	}
 * @author Darker.Wang
 *
 */
public class AccessFilter extends ZuulFilter {

	private static Logger log = LoggerFactory.getLogger(AccessFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();

        log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));

        Object accessToken = request.getParameter("accessToken");
        if(accessToken == null) {
        	//TODO token 验证:暂时停用。释放开启，需要每个请求都添加token
            log.warn("access token is empty");
            //ctx.setSendZuulResponse(false);
            //ctx.setResponseStatusCode(401);
            //return null;
        }
        log.info("access token ok");
        return null;
    }

}
